package com.jianwei.service.generator;

import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractIdGeneratorFactory implements IdGeneratorFactory {

    private static final ConcurrentHashMap<String, IdGenerator> ID_GENERATOR_MAP = new ConcurrentHashMap<>();

    @Override
    public IdGenerator getIdGenerator(String businessType) {
        //双重判断
        if (ID_GENERATOR_MAP.containsKey(businessType)) {
            return ID_GENERATOR_MAP.get(businessType);
        }
        synchronized (this) {
            if (ID_GENERATOR_MAP.containsKey(businessType)) {
                return ID_GENERATOR_MAP.get(businessType);
            }
            IdGenerator idGenerator = createIdGenerator(businessType);
            ID_GENERATOR_MAP.put(businessType, idGenerator);
            return idGenerator;
        }
    }

    protected abstract IdGenerator createIdGenerator(String businessType);
}
